package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.BicycleOrderBo;
import com.groupseven.happycampus.common.pojo.BicycleOrder;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.BicycleOrderMapper;
import com.groupseven.happycampus.service.BicycleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("bicycleOrderService")
public class BicycleOrderServiceImpl implements BicycleOrderService {

    @Autowired
    private BicycleOrderMapper bicycleOrderMapper;

    @Override
    public LayuiPageVo<BicycleOrder> bicycleOrderListPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<BicycleOrder> bicycleOrders = bicycleOrderMapper.bicycleOrderListSelect(params);

        //将获取到的 bicycles 集合赋值给 PageInfo 类型的 bicyclePageInfo 对象
        PageInfo<BicycleOrder> bicycleOrderPageInfo = new PageInfo<>(bicycleOrders);

        //创建一个 LayuiPageVo 类型的 bicycleLayuiPageVo 对象，用来存放后面的 R 类型的数据
        LayuiPageVo<BicycleOrder> bicycleOrderLayuiPageVo = new LayuiPageVo<>();
        bicycleOrderLayuiPageVo.setCode(0);
        bicycleOrderLayuiPageVo.setMsg("分页操作");
        bicycleOrderLayuiPageVo.setCount(bicycleOrderPageInfo.getTotal());
        bicycleOrderLayuiPageVo.setData(bicycleOrderPageInfo.getList());

        return bicycleOrderLayuiPageVo;
    }

    @Override
    public int bicycleOrderAdd(BicycleOrder bicycleOrder) {
        int result = bicycleOrderMapper.bicycleOrderAdd(bicycleOrder);
        return result;
    }

    @Override
    public int bicycleOrderUpdate(BicycleOrder bicycleOrder) {
        return bicycleOrderMapper.bicycleOrderUpdate(bicycleOrder);
    }

    @Override
    public int bicycleOrderDelete(Integer id) {
        return bicycleOrderMapper.bicycleOrderDelete(id);
    }

    @Override
    public List<BicycleOrderBo> orderExcelData(BicycleOrder bicycleOrder) {
        List<BicycleOrderBo> orderBoList = bicycleOrderMapper.orderExcelData(bicycleOrder);
        List<BicycleOrderBo> collect = orderBoList.stream().map(orderBo -> {
            BicycleOrderBo bicycleOrderBo = new BicycleOrderBo(orderBo.getOrderId(), orderBo.getBicycleOrderStart(),
                    orderBo.getBicycleOrderEnd(), orderBo.getRentTime(), orderBo.getPrice(), orderBo.getAmountMoney());
            return bicycleOrderBo;
        }).collect(Collectors.toList());
        return collect;
    }
}
